#define device_type          @"device_type"
#define device_rssi          @"device_rssi"
#define device_name          @"device_name"
#define device_mac           @"device_mac"
#define device_tx_power      @"device_tx_power"
#define device_battery       @"device_battery"
#define device_interval      @"device_interval"
#define device_beacon_uuid   @"device_beacon_uuid"
//为标准beacon,可进行密码、UUID服务码、MAC地址、major和minor
#define device_isStandardBeacon      @"device_isStandardBeacon"
#define device_major         @"device_major"
#define device_minor         @"device_minor"
#define device_temperature   @"device_temperature"//温度
#define device_humidity      @"device_humidity"//湿度
#define device_pressure      @"device_pressure"//气压
#define device_shake         @"device_shake"//20s振动
#define device_direction     @"device_direction"//朝向
#define device_button_state  @"device_button_state"//是否按下

#define device_UID_NamespaceID   @"UID_NamespaceID" //eddystone UID_NamespaceID
#define device_UID_InstanceID    @"UID_InstanceID"       //eddystone UID_Instance
#define device_URL_content       @"URL_content"       //eddystone URL
#define device_TLM_version       @"TLM_version"
#define device_TLM_temperature   @"TLM_temperature"
#define device_TLM_voltage       @"TLM_voltage"
#define device_TLM_PDU           @"TLM_PDU"
#define device_TLM_timeSincePowerUp  @"TLM_timeSincePowerUp"
#define device_EID_ID            @"EID_id"
#define device_Eddystone_type    @"Eddystone_type"
#define device_UID_Adv_state     @"UID_Adv_state"
#define device_URL_Adv_state     @"URL_Adv_state"
#define device_EID_Adv_state     @"EID_Adv_state"
#define device_TLM_Adv_state     @"TLM_Adv_state"
//#define kDeviceInfoDidUpdate @"DeviceInfoDidUpdate"

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "DeviceModel.h"
@import iOSDFULibrary;

typedef void (^connectPeripheralBLK) (NSMutableArray *connectPeripherals);
typedef void(^opFail)(NSString *errStr);
typedef void(^opSucess)(void);
typedef NS_ENUM(NSInteger,DeviceType){
    DeviceTypeBeacon = 0,
    DeviceTypeiBeacon,
    DeviceTypeEddystone
} ;
@protocol BluetoothCenterDelegate <NSObject>
@optional
//搜索到了外设
- (void)bluetoothCenterDidScandPeripheral:(CBPeripheral *)peripheral;
//连接蓝牙成功
-(void)bluetoothDidConnectedPeripheral:(CBPeripheral*)peripheral;
//扫描到特征
-(void)didDiscoverCharacteristicsForService:(CBPeripheral*)peripheral;
//连接失败,超时
-(void)bluetoothCenterDidFailToConnnectPeripheral;
//断开蓝牙成功
-(void)bluetoothDisConnectPeripheral:(CBPeripheral*)peripheral;
//写数据回调
- (void)writeDataCallBack:(NSString *)callStr;
//接收信号强度
-(void)receiveRSSI:(NSString*)RSSI peripheral:(CBPeripheral*)peripheral;
-(void)receiveBattery:(NSString*)batteryLevel;
//发现新设备,刷新设备列表
-(void)refreshTableView;
//输入密码正确
-(void)verifyPwdSuccess;
-(void)deviceDidUpdateInfo:(NSDictionary*)info;
@end
@interface BluetoothCenter : NSObject
@property (nonatomic, copy) connectPeripheralBLK peripheralBlk;
@property (nonatomic, weak) id <BluetoothCenterDelegate> delegate;
@property(nonatomic,assign)DeviceType deviceType;
@property(nonatomic,assign)DeviceType willChangeType;//标记将要切换的广播,以便切换成功后使用
@property (nonatomic, strong) CBPeripheral *currentPeripheral;//当前外设
@property (strong, nonatomic) CBCentralManager *centralManager;//中心端
@property (nonatomic, copy) NSString *currentGear;
@property (nonatomic, assign) NSInteger sendNumber;
@property (nonatomic, copy) NSString *readValueC;
@property (nonatomic, assign) BOOL isReadValueC;
///扫描到的外设
@property (nonatomic, strong) NSMutableArray<CBPeripheral *> *peripherals;
///已经连接的外设
@property (nonatomic, strong) NSMutableArray *connectSuccessPeripherals;
@property (assign, nonatomic) BOOL isManualDisconnect;//是否手动断开连接
@property (nonatomic, assign) BOOL isBluetoothOpen;//蓝牙是否打开
@property (nonatomic, assign) BOOL isConnectPeripheral;//是否连接了外设
@property (nonatomic, assign) BOOL isSupportHeat;//是否支持加热
@property (nonatomic, assign) BOOL isNewVersion;//是否新版本

@property(nonatomic,assign)BOOL isFilterRSSI;
@property(nonatomic,assign)int rssi_filter;//过滤的信号值,带负号
@property(nonatomic,assign)BOOL isFilterMajor;
@property(nonatomic,assign)BOOL isFilterMinor;
@property(nonatomic,assign)int majorMin_filter;//过滤的major最小值
@property(nonatomic,assign)int majorMax_filter;//过滤的major最大值
@property(nonatomic,assign)int minorMin_filter;//过滤的minor最小值
@property(nonatomic,assign)int minorMax_filter;//过滤的minor最大值
@property(nonatomic,copy)NSString *filterString;//过滤的蓝牙名字
//存储当前搜索到的设备的一些信息
@property(nonatomic,strong)NSMutableDictionary *deviceInfo;
//格式:{@"设备标识":@{@"major":@"",@"minor":@""},@"battery":@"",.....}

@property(nonatomic,assign)BOOL isConnecting;//是否正在连接
@property(nonatomic,strong)DFUFirmware *firmware;//升级文件
@property(nonatomic,copy) NSString *writeCharacteristics;//写特征
@property(nonatomic,copy)NSString *dfu_name;
@property(nonatomic,copy)NSString *dfu_code;
+ (instancetype)shareInstance;
- (void)scanperipheral;
///扫描指定的外设
- (void)startScanningPrivate:(NSArray *)uuidStrs;
///连接蓝牙
- (void)connetPeripheral:(CBPeripheral *)peripheral;
//断开蓝牙
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;
//断开当前连接的蓝牙
-(void)disconnectCurrentPeripheral;
//断开所有蓝牙
- (void)disconnectAllPeripheral;
- (void)disConnectPeripheral:(opSucess)sucess
                      opFail:(opFail)fail;
- (void)reConnectOpSucess:(opSucess)sucess opFail:(opFail)fail;

- (void)stopScand;
- (void)clearConnectSuccessPeripherals;

- (void)stringWritesToBluetoothData :(NSString *)dataStr;
- (void)startUpdateTimer;//开启读取信号强度和电量定时器
- (void)stopUpdateTimer;
- (void)updateRSSIAndBattery;//读取信号强度和电量
- (void)checkVersion;//检查新老版本
- (void)getFirmwareVersion;//获取固件版本号
- (void)startConnectTimer;
- (void)stopConnectTimer;

-(void)sendDataToDevice:(NSData*)data;
@end
